#include "mytimer.h"
#include <iostream>
#include <QStack>
#include <QByteArray>


MyTimer::MyTimer(QObject *parent)
    : QObject{parent}
{
    pT1 = new QTimer(this);
    pT2 = new QTimer(this);
    pT3 = new QTimer(this);
    connect(pT1, &QTimer::timeout, this, &MyTimer::getLiveMp4s);
    connect(pT2, &QTimer::timeout, this, &MyTimer::killDeadMp4s);
    connect(pT3, &QTimer::timeout, this, &MyTimer::print);
}

void MyTimer::start()
{
    pT1->start(3000);
    pT2->start(3000);
    pT3->start(3000);
}

void MyTimer::getLiveMp4s() {
//    std::cout << "MyTimer::getLiveMp4s()" << std::endl;
    QStack<QDir> stack;
    QVector<Mp4> mayLiveMp4s;
    for (QString dirName : config.dirs) {
        QDir dir(dirName);
        if (!dir.exists()) {
            std::cout << "dir {" << dirName.toStdString() << "} unknown." << std::endl;
        } else {
            stack.push(dir);
        }
    }
    while (stack.size() != 0) {
        QDir dir = stack.pop();
        dir.setFilter(QDir::Files |
                      QDir::Dirs |
                      QDir::NoSymLinks |
                      QDir::NoDotAndDotDot);
        QFileInfoList list = dir.entryInfoList();
        for (const QFileInfo &info : list) {
            if (info.isFile() && rMp4.match(info.fileName()).hasMatch()) {
                allMp4NameMutex.lock();
                if (!allMp4Name.contains(info.absoluteFilePath())) {
                    allMp4Name.emplace_back(info.absoluteFilePath());
                    mayLiveMp4s.emplace_back(Mp4(info, info.size()));
                }
                allMp4NameMutex.unlock();
            } else if (info.isDir()) {
                stack.push(QDir(info.absoluteFilePath()));
            }
        }
    }
//    if (mayLiveMp4s.size() != 0) std::cout << "mayLiveMp4s:" << std::endl;
//    for (QVector<Mp4>::iterator it = mayLiveMp4s.begin(); it != mayLiveMp4s.end(); it++) {
//        std::cout << QSTR_TO_STR(it->fileInfo.absoluteFilePath()) << " " << it->fileInfo.size() << std::endl;
//    }
    QThread::msleep(1000);
    if (filterLiveMp4s(mayLiveMp4s) != 0) {
        liveMp4sMutex.lock();
        liveMp4s += mayLiveMp4s;
//        std::cout << "liveMp4s:" << std::endl;
//        for (QVector<Mp4>::iterator it = liveMp4s.begin(); it != liveMp4s.end(); it++) {
//            std::cout << QSTR_TO_STR(it->fileInfo.absoluteFilePath()) << " " << it->fileInfo.size() << std::endl;
//        }
        liveMp4sMutex.unlock();
    }
}

void killDeadMp4(Mp4 mp4) {
//    for (QVector<Tag>::iterator it = mp4.tags.begin(); it != mp4.tags.end(); it++) {
//        if (it->time.secsTo(mp4.fileInfo.lastModified()) < 0) {
//            mp4.tags.erase(it, mp4.tags.end());
//            break;
//        }
//    }
    deadMp4sMutex.lock();
    deadMp4s.emplace_back(mp4);
    deadMp4sMutex.unlock();
}

void MyTimer::killDeadMp4s() {
//    std::cout << "MyTimer::killDeadMp4s()" << std::endl;
    liveMp4sMutex.lock();
//    std::cout << "liveMp4sMutex.lock();" << std::endl;
    updateLiveMp4sSize(liveMp4s);
    liveMp4sMutex.unlock();
//    std::cout << "liveMp4sMutex.unlock();" << std::endl;
    QThread::msleep(1000);
    liveMp4sMutex.lock();
//    std::cout << "liveMp4sMutex.lock();" << std::endl;
    for (QVector<Mp4>::iterator it = liveMp4s.begin(); it != liveMp4s.end();) {
        it->fileInfo.refresh();
        if (it->fileInfo.size() == it->size) {
//            std::cout << "deadMp4: " << QSTR_TO_STR(it->fileInfo.absoluteFilePath()) << " " << it->size << std::endl;
            killDeadMp4(*it);
            it = liveMp4s.erase(it);
//            deadMp4sMutex.lock();
//            if (deadMp4s.size() != 0) std::cout << "deadMp4s:" << std::endl;
//            for (QVector<Mp4>::iterator it = deadMp4s.begin(); it != deadMp4s.end(); it++) {
//                std::cout << QSTR_TO_STR(it->fileInfo.absoluteFilePath()) << " " << it->size << std::endl;
//            }
//            deadMp4sMutex.unlock();
        } else {
            it++;
        }
    }
    liveMp4sMutex.unlock();
    //    std::cout << "liveMp4sMutex.unlock();" << std::endl;
}

void MyTimer::print()
{
//    std::cout << "MyTimer::print()" << std::endl;
    liveMp4sMutex.lock();
    std::cout << "liveMp4s:" << std::endl;
    for (QVector<Mp4>::iterator it = liveMp4s.begin(); it != liveMp4s.end(); it++) {
        std::cout << QSTR_TO_STR(it->fileInfo.absoluteFilePath()) << " " << it->size << std::endl;
    }
    liveMp4sMutex.unlock();

    deadMp4sMutex.lock();
    std::cout << "deadMp4s:" << std::endl;
    for (QVector<Mp4>::iterator it = deadMp4s.begin(); it != deadMp4s.end(); it++) {
        std::cout << QSTR_TO_STR(it->fileInfo.absoluteFilePath()) << " " << it->size << std::endl;
    }
    deadMp4sMutex.unlock();
    std::cout << std::endl;
}
